package 多线程练习;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolTest {
      public static void main(String[] args) {
            ExecutorService service = Executors.newFixedThreadPool(10);

            service.submit(new Runnable() {
                  @Override
                  public void run() {
                        synchronized (this) {
                              for(int i=1;i<=100;i++){
                                    notify();
                                    if(i%2==0){
                                          System.out.println("这个线程打印偶数"+i);
                                          //打印完偶数需要这个线程进行下阻塞并开锁
                                          try {
                                                wait();
                                          } catch (InterruptedException e) {
                                                throw new RuntimeException(e);
                                          }
                                    }
                              }
                        }
                  }
            });

            service.execute(new Runnable() {
                  @Override
                  public void run() {
                        synchronized (this) {
                              for(int i=1;i<=100;i++){
                                    notify();
                                    if(i%2==1){
                                          System.out.println("这个线程打印奇数"+i);
                                          try {
                                                wait();
                                          } catch (InterruptedException e) {
                                                throw new RuntimeException(e);
                                          }
                                    }
                              }
                        }
                  }
            });
      }
}
